//
//  HMNetworkTools.m
//  OC-NetworkTools
//
//  Created by 范英强 on 16/1/28.
//  Copyright © 2016年 范英强. All rights reserved.
//

#import "HMNetworkTools.h"


@protocol hmDelegate <NSObject>

- (NSURLSessionDataTask *)dataTaskWithHTTPMethod:(NSString *)method
                                       URLString:(NSString *)URLString
                                      parameters:(id)parameters
                                  uploadProgress:(nullable void (^)(NSProgress *uploadProgress)) uploadProgress
                                downloadProgress:(nullable void (^)(NSProgress *downloadProgress)) downloadProgress
                                         success:(void (^)(NSURLSessionDataTask *, id))success
                                         failure:(void (^)(NSURLSessionDataTask *, NSError *))failure;

@end

@interface HMNetworkTools()<hmDelegate>

@end
@implementation HMNetworkTools

+ (instancetype)sharedTools{
    static HMNetworkTools *instance;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        instance = [HMNetworkTools manager];
        instance.responseSerializer.acceptableContentTypes = [NSSet setWithObject:@"text/html"];
    });
    return instance;
}

- (void)requset:(HMNetworkToolsMethod)method url:(NSString *)url params:(id)params success:(void (^)(id responseObject))success failure:(void(^)(NSError *error))failure{
    
    
    NSString *thd = method == GET ? @"GET" : @"POST";
    //在oc中的方法是动态想对象身上发送消息
    // 在接收到这个消息dataTaskWithHTTPMethod
    //利用 oc动态派发的的机制
    [[self dataTaskWithHTTPMethod:thd URLString:url parameters:params uploadProgress:nil downloadProgress:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        success(responseObject);
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        failure(error);
    }] resume];
    
//    AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
//    //设置可接受类型
//    
//    if (method == GET) {
//        //get   请求
//        [manager GET:@"http://www.weather.com.cn/data/sk/101010100.html" parameters:nil progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
//            success(responseObject);
//            
//        } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
//            failure(error);
//        }];
//    }else {
//        //get   请求
//        [manager POST:@"http://www.weather.com.cn/data/sk/101010100.html" parameters:nil progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
//            NSLog(@"成功%@",responseObject);
//            
//        } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
//            NSLog(@"失败%@",error);
//        }];
//    }
    
    
}

/**
 NSURLSessionDataTask *dataTask = [self dataTaskWithHTTPMethod:@"GET"
 URLString:URLString
 parameters:parameters
 uploadProgress:nil
 downloadProgress:downloadProgress
 success:success
 failure:failure];
 
 NSURLSessionDataTask *dataTask = [self dataTaskWithHTTPMethod:@"POST" URLString:URLString parameters:parameters uploadProgress:uploadProgress downloadProgress:nil success:success failure:failure];

 */

@end
